#!/usr/bin/perl
#vol_detail.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $volname = $in{'volname'};
#my $tmp_volname = get_volname($volname);
my $tmp_volname = $volname;
if (!$volname) {
	#volname can't be nulls
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

my (@rdinfo, @rdsinfo, $r, $rr);

my $mkfs_lock;
$mkfs_lock = check_lock();

if ($mkfs_lock == 2) {
	@rdinfo = read_stale_raid_config();
}
else {
	@rdinfo = new_get_check_raid_info();
}

my ($i);

my $found = 0;
foreach $r (@rdinfo) {
	if ($r->{'name'} eq $tmp_volname) {
		$rr = $r;
		$found= 1;
		last;
	}
}

if (!$found) {
	#finish latter
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

my @dinfo;

@dinfo=&get_diskindex();

&header($text{'vol_detail_title'}, "", "vol_detail", 0, 1);

print "<table class=group border=1 cellpadding=4 cellspacing=0 width=100%>\n";

print "<tr class=title1> <td class=group> <b>$text{'dl_volname'}$rr->{'volname'}</b>",
	"</td></tr>\n";
print "<tr> <td class=group><table class=mode1 cellpadding=3 cellspacing=0",
	"width=100%>\n";

#raid level
print "<tr> <td width=250> <b> $text{'dl_level'} </b> </td> \n";
print "<td> $rr->{'raidlevel'} </td> </tr> \n";

#physical status
print "<tr> <td width=250> <b> $text{'dl_pstatus'} </b> </td> \n";
print "<td>";
if ($rr->{'resync'}) {
	if ($rr->{'resync_delayed'}) {
		print $text{'dl_delayed'};
	}
	else {
		print &text('dl_resync', $rr->{'percent'},
			$rr->{'remaining'}, $rr->{'speed'});
	}
}
elsif ($rr->{'recovery'}) {
	print &text('dl_recovery_new', $rr->{'percent'});
}
else {
	#normal status
	print $text{'dl_normal'};
}
print "</td></tr>\n";

# integrity
print "<tr> <td width=250> <b> $text{'dl_integrity'} </b> </td> \n";
print "<td>";

if ($rr->{'raid_disks'} != $rr->{'working_disks'}) {
	my $failed_disks = $rr->{'raid_disks'} - $rr->{'working_disks'};
	print &text('dl_unintegrated', $failed_disks);
}
else {
	print $text{'dl_integrated'};
}
print "</td></tr>\n";

# Required good disks number
#print "<tr> <td width=250> <b> $text{'dl_rqdisk'} </b> </td> \n";
#print "<td> $rr->{'raid_disks'} </td> </tr> \n";
my($rdev, @rdisknames, $tmp);

# contained disks
print "<tr> <td width=250> <b> $text{'dl_cdisk'} </b> </td> \n";
print "<td>";

#foreach $rdev (@{$rr->{'rdevs'}}) {
#	$tmp = "disk".rdev2disk(\@dinfo,$rdev);
#	push(@rdisknames, $tmp);
#}

#$tmp = join(', ' , sort(@rdisknames));
@tmpd = get_unit_disks($rr->{'name'});
$tmp = join(', ' , sort(@tmpd));
print &text('dl_condisk1', $tmp);
print "</td></tr>\n";
undef(@rdisknames);

# Good disks
for ($i = 0; $i < @{$rr->{'rdevs'}}; $i++) {
	if ($rr->{'rdevs_role'}->[$i] eq 'good') {
		$tmp = "disk".rdev2disk(\@dinfo,$rr->{'rdevs'}->[$i]);
		push(@rdisknames, $tmp);
	}
}

if (@rdisknames) {
	print "<tr> <td width=250> <b> $text{'dl_gdisk'} </b> </td> \n";
	$tmp = join(", ", sort(@rdisknames));
	print "<td> $tmp </td> </tr> \n";
}
undef(@rdisknames);

# Failed disks
for ($i = 0; $i < @{$rr->{'rdevs'}}; $i++) {
	if ($rr->{'rdevs_role'}->[$i] eq 'failed') {
		$tmp = "disk".rdev2disk(\@dinfo,$rr->{'rdevs'}->[$i]);
		push(@rdisknames, $tmp);
	}
}

print @rdisknames;
if (@rdisknames) {
	print "<tr> <td width=250> <b> $text{'dl_fdisk'} </b> </td> \n";
	$tmp = join(", ", sort(@rdisknames));
	print "<td> $tmp </td> </tr> \n";
}
undef(@rdisknames);

# Spare disks
for ($i = 0; $i < @{$rr->{'rdevs'}}; $i++) {
	if ($rr->{'rdevs_role'}->[$i] eq 'spare') {
		$tmp = "disk".rdev2disk(\@dinfo,$rr->{'rdevs'}->[$i]);
		push(@rdisknames, $tmp);
	}
}

if (@rdisknames) {
	print "<tr> <td width=250> <b> $text{'dl_sdisk'} </b> </td> \n";
	$tmp = join(", ", sort(@rdisknames));
	print "<td> $tmp </td> </tr> \n";
}
undef(@rdisknames);

# Removed disks
for ($i = 0; $i < @{$rr->{'rdevs'}}; $i++) {
	if ($rr->{'rdevs_role'}->[$i] eq 'removed') {
		$tmp = "disk".rdev2disk(\@dinfo,$rr->{'rdevs'}->[$i]);
		push(@rdisknames, $tmp);
	}
}

if (@rdisknames) {
	print "<tr> <td width=250> <b> $text{'dl_rmdisk'} </b> </td> \n";
	$tmp = join(", ", sort(@rdisknames));
	print "<td> $tmp </td> </tr> \n";
}
undef(@rdisknames);

# Unknown disks
for ($i = 0; $i < @{$rr->{'rdevs'}}; $i++) {
	if ($rr->{'rdevs_role'}->[$i] eq 'unknown') {
		$tmp = "disk".rdev2disk(\@dinfo,$rr->{'rdevs'}->[$i]);
		push(@rdisknames, $tmp);
	}
}

if (@rdisknames) {
	print "<tr> <td width=250> <b> $text{'dl_unknown'} </b> </td> \n";
	$tmp = join(", ", sort(@rdisknames));
	print "<td> $tmp </td> </tr> \n";
}
@rdisknames=undef;

# valid physical size(blocks)
print "<tr> <td width=250> <b> $text{'dl_physize'} </b> </td> \n";
print "<td>";
my($sizeM);
$sizeM = int($rr->{'size'}*1024);
print &text('dl_sizeM', $sizeM);
print "</td></tr>\n";

#//print "<tr> <td width=250> <b> $text{'dl_volstatus'} </b> </td> \n";
if (($rr->{'status'} eq 'OK') || ($rr->{'status'} eq 'REBUILDING') || ($rr->{'status'} eq 'DEGRADED')){
	my ($vol_sizeM, $vol_usedM, $vol_freeM, $vol_usedper);
	$vol_sizeM = int($rr->{'vol_size'}/1024);
	$vol_usedM = int($rr->{'vol_used'}/1024);
	$vol_freeM = int($rr->{'vol_free'}/1024);
	$vol_usedper = $rr->{'vol_usedper'};
	#///print "<td> $text{'dl_volnormal'}";
	#///print "</td></tr>\n";
	#///print "<tr> <td width=250> <b> $text{'dl_volusage'} </b> </td> \n";
	#///print "<td>", &text('dl_volusage1', $vol_sizeM, 
	#///			$vol_usedM, $vol_freeM, $vol_usedper);
	#///print "</td></tr>\n";
}
elsif ($rr->{'status'} eq 'new') {
	#///print "<td> $text{'dl_volnew'}";
	#///print "</td></tr>\n";
}
elsif ($rr->{'status'} eq 'rebuild') {
	#///print "<td> $text{'dl_volbuilding'}";
	#///print "</td></tr>\n";
}
else {
	#///print "<td> $text{'dl_volunknown'}";
	#///print "</td></tr>\n";
}

print "</td> </tr> \n";
print "</table>\n";

print "</table>\n";

&footer("/rsman/", $text{'index'});

return;

ERROR_OUT:
&error_setup($text{'dl_err_title'});
&error($err_msg);
		

